//
//  videoCollectionViewCell.m
//  XWZ_News_Work
//
//  Created by dllo on 16/1/11.
//  Copyright © 2016年 dllo. All rights reserved.
//


#import "videoCollectionViewCell.h"
#import "videoHeaderCollectionViewCell.h"

@interface videoCollectionViewCell()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) UITableView *tableV;

#pragma headV
@property (nonatomic, strong) UICollectionView *collectionHeadView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowL;

#pragma 重用池保存indexPath
@property (nonatomic, assign) NSInteger myIndexPathRow;


@end

@implementation videoCollectionViewCell

- (void)setClassifyArr:(NSMutableArray *)classifyArr
{
    if (_classifyArr != classifyArr) {
         _classifyArr = classifyArr;
    }
    [self.tableV.mj_header endRefreshing];
    [self.tableV.mj_footer endRefreshing];
    [self.collectionHeadView reloadData];

}

- (void)setVideoArr:(NSMutableArray *)videoArr
{
    if (_videoArr != videoArr) {
        _videoArr = videoArr;
    }
    [self.tableV reloadData];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews
{
    [self createTableView];
    [self createHeaderView];
}

#pragma 创建headView
- (void)createHeaderView
{
    self.flowL = [[UICollectionViewFlowLayout alloc] init];
    self.flowL.itemSize = CGSizeMake(80, 100);
    self.flowL.sectionInset = UIEdgeInsetsMake(10, 5, 5, 5);
    self.flowL.minimumInteritemSpacing = 10;
    self.collectionHeadView = [[UICollectionView alloc] initWithFrame:CGRectMake(20, 20, SCREEN_WIDTH - 40, 120) collectionViewLayout:self.flowL];
    [self.collectionHeadView.layer setBorderWidth:5.0];
    self.collectionHeadView.layer.borderColor = [UIColor colorWithRed:224 / 255.0 green:221 / 255.0 blue:224 / 255.0 alpha:1.0].CGColor;
    self.collectionHeadView.delegate = self;
    self.collectionHeadView.dataSource = self;
    self.collectionHeadView.backgroundColor = [UIColor whiteColor];
    
    [self.collectionHeadView registerClass:[videoHeaderCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    self.tableV.tableHeaderView = self.collectionHeadView;
    
    //下拉
    self.tableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
    [self.tableV.mj_header beginRefreshing];
    
    
    //上拉
    self.tableV.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreData)];
    [self.tableV.mj_footer beginRefreshing];
}

//下拉
- (void)requestData
{
    [self.delegate refreshData];
}

//上拉
- (void)getMoreData
{
    [self.delegate getMore];
}

#pragma collectionView cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.classifyArr.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
#pragma
    [[avTools sharePlayer] pause];
    videoModel *model = [self.classifyArr objectAtIndex:indexPath.row];
    [self.delegate getSid:model.sid];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    videoModel *model = [self.classifyArr objectAtIndex:indexPath.row];
    videoHeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    [cell.imageHead sd_setImageWithURL:[NSURL URLWithString:model.imgsrc] placeholderImage:[UIImage imageNamed:@"lol-tupian"]];
    cell.titleLab.text = model.title;
    return cell;
}

#pragma 创建tableView
- (void)createTableView
{
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 12, SCREEN_WIDTH, SCREEN_HEIGHT - SCREEN_HEIGHT / 9) style:(UITableViewStyleGrouped)];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.contentView addSubview:self.tableV];
}

#pragma Number row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.videoArr.count;
}

#pragma cellForRow
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    videoModel *model = [self.videoArr objectAtIndex:indexPath.row];
    static NSString *cellStr = @"cell";
    videoTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellStr];
    if (nil == cell) {
        cell = [[videoTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:cellStr];
    }
    
    if (self.myIndexPathRow) {
    
        if (self.myIndexPathRow == indexPath.row) {
            
            [cell.contentView.layer addSublayer:cell.playerLayer];


            
        }else{
            [cell.playerLayer removeFromSuperlayer];
        }
    }

    [cell.imgPic sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:[UIImage imageNamed:@"lol-tupian"]];
    cell.selectionStyle = NO;
    cell.titleLab.text = model.title;
    cell.desLab.text = model.descrip;
#pragma playtime
    NSInteger min = model.length / 60;
    NSInteger hour = model.length / 360;
    NSInteger remainder = model.length % 60;
    cell.timeLab.text = [NSString stringWithFormat:@"%ld%ld:%02ld", (long)hour, (long)min, remainder];

#pragma playCount
    if (10000 < model.playCount) {
        
    NSString *firstA = [[NSString stringWithFormat:@"%ld", (long)model.playCount] substringToIndex:1];
        NSString *firstB = [[NSString stringWithFormat:@"%ld", (long)model.playCount] substringWithRange:NSMakeRange(1, 1)];
    cell.playcountLab.text = [NSString stringWithFormat:@"%@.%@万", firstA, firstB];
        
    } else {
    cell.playcountLab.text = [NSString stringWithFormat:@"%ld", (long)model.playCount];
    }
#pragma commentCount
    if(0 == model.replyCount)
    {
     cell.wiriteLab.text = nil;
    } else {
     cell.wiriteLab.text = [NSString stringWithFormat:@"%ld", (long)model.replyCount];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    videoTableViewCell *videoCell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (self.myIndexPathRow == indexPath.row) {
        
        if ([avTools sharePlayer].rate == 0) {
            [[avTools sharePlayer] play];
#pragma 消息通知停止音乐
            [[NSNotificationCenter defaultCenter] postNotificationName:@"stopMusic" object:nil];
            
        }else{
            [[avTools sharePlayer] pause];
        }
        
    }else{
        
        videoTableViewCell *oldCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.myIndexPathRow inSection:0]];
        [oldCell.playerLayer removeFromSuperlayer];
        videoModel *model = [self.videoArr objectAtIndex:indexPath.row];
        self.myIndexPathRow = indexPath.row;
        videoCell.videoM = model;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 370;
}

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//
//    if (self.myIndexPathRow) {
//        if (scrollView.contentOffset.y > (self.myIndexPathRow + 1 ) * 370) {
//            
//            smallView *view = [smallView createView];
//            [self.contentView addSubview:view];
//            
//        } else {
//            
//            smallView *view = [smallView createView];
//            [view removeFromSuperview];
//    
//        }
//    }
//
//}



@end
